(function () {
    let form = document.forms.loginForm,
        nmsg = document.getElementById('message'),
        divs = document.querySelectorAll('.txt');

    function md5(msg) {
        return msg;
    }

    function showMessage(clazz, msg) {
        if (!clazz) {
            // nmsg.innerHTML = '';
            nmsg.classList.remove('j-suc');
            nmsg.classList.remove('j-err');
        } else {
            nmsg.innerHTML = msg;
            nmsg.classList.add(clazz);
        }
    }

    function disableSubmit(disabled) {
        form.loginBtn.disabled = !!disabled;
        let method = !disabled ? 'remove' : 'add';
        form.loginBtn.classList[method]('j-disabled');
    }

    function invalidInput(node, msg) {
        showMessage('j-err', msg);
        node.classList.add('j-error');
        node.focus();
    }

    function clearInvalid(node) {
        showMessage();
        node.classList.remove('j-error');
    }

    form.name.addEventListener(
        'invalid', function (event) {
            event.preventDefault();
            let input = form.name;
            invalidInput(input, '账号不能为空');
        }
    );

    form.addEventListener(
        'input', function (event) {
            // 还原错误状态
            clearInvalid(event.target);
            // 还原登录按钮状态
            disableSubmit(false);
        }
    );

    form.addEventListener(
        'submit', function (e) {
            // 密码验证
            let input = form.password,
                pswd = input.value,
                emsg = '';
            if (pswd.length < 6) {
                emsg = '密码长度最少6位';
            }
            if (!!emsg) {
                e.preventDefault();
                invalidInput(input, emsg);
                return;
            }
            input.value = md5(pswd);
            // 禁用提交按钮
            disableSubmit(true);


            //阻止表单的默认提交行为
            e.preventDefault();

            //采集用户输入的信息
            const data = $(form).serialize()
            console.log(data)

            //发送ajax请求
            $.post('http://hn216.api.yesapi.cn/?s=App.User.LoginExt', data, res => {
                console.log(res)
                //判断返回结果
                if (res.data.err_code === 1) {
                    showMessage('j-err', '该账号不存在！');
                    return;
                } else if (res.data.err_code === 2) {
                    showMessage('j-err', '密码错误！');
                    return;
                }else if (res.data.err_code === 0) {
                    //1.把登录过的"凭证"存储起来，给其他页面使用
                    window.localStorage.setItem('token',res.data.token);2

                    //2.把用户的uuid信息也存储起来
                    window.localStorage.setItem('uuid',res.data.uuid);

                    //3.移除输入框和提示去注册
                    divs.forEach(function (item) {
                        item.remove();
                    })
                    showMessage('j-suc', '恭喜您！登录成功！<br>即将跳回首页...');
                    //4.隔一秒返回首页
                    setTimeout(function () {
                        window.location.href = '../html/index.html'
                    },1000)
                }
            })
        }
    );
})();